package controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import model.ButtonRequest;
import model.ButtonRequestModel;
import model.UnregisteredUser;
import view.PendingRequestDetailsGUI;
import view.PendingRequestsGUI;
import client.CliMessage;


/**
 * This Class is a Pending Request Controller Registration which extends AbstractController
 * @author Tal
 *
 */

public class PendingRegistrationRequestsController extends AbstractController
{
		//private
			private PendingRequestsGUI PendingRequestView;
			private ButtonRequestModel ButtonRequestModel;
		
			/**
			 * This is Pending Registration Requests Controller constructor.
			 * @param PendingRequestView is a Pending Request Registration window GUI instance.
			 * @param ButtonRequestModel is a ButtonRequest Model instance.
			 */
			public PendingRegistrationRequestsController(PendingRequestsGUI PendingRequestView, ButtonRequestModel ButtonRequestModel) 
			{
				super(PendingRequestView,ButtonRequestModel);
				this.PendingRequestView = PendingRequestView;
				this.ButtonRequestModel = ButtonRequestModel;		
			}
					
			
			/**
			 * This is setRequestsButtons method-this method add listeners to buttons in Pending Request window.
			 * 
			 */
			
			private void setRequestsButtons(ArrayList<ButtonRequest>  btnReq)
			{
				
				int nButtons = btnReq.size();
				
				//set a HashMap that contains all possible button listeners by their names
				ArrayList<ButtonListener> al = new ArrayList<ButtonListener>();
				ArrayList<ActionListener> btnList = new ArrayList<ActionListener>();
				
				//set  specific complaints buttons
				for(int i = 0; i < nButtons ; i++)
				{
					String reqID = btnReq.get(i).getButtonReqID();
					String uid = btnReq.get(i).getButtonUID();
					al.add(new ButtonListener(this,reqID,uid));
					btnList.add(al.get(i));
					PendingRequestView.addButtonListener(btnList.get(i),i);
				}	
			
			}
			
			/**
			 * Inner class that handles when Button Pressed by request Id, extends AbstractListener
			 * @author Tal
			 *
			 */
			
			class ButtonListener extends AbstractListener
			{
				private PendingRegistrationRequestsController previusObserver;
				private String ReqID;
				private String Uid;
				
				public ButtonListener(PendingRegistrationRequestsController previusObserver, String reqid,String uid)
				{
					this.ReqID=reqid;
					this.Uid=uid;
					this.previusObserver = previusObserver;
				}
				
				@Override
				public void actionPerformed(ActionEvent arg0) 
				{
					//create a new PendingRegistrationRequestDetails controller
					PendingRequestDetailsGUI PendingRequestDetailsView = new PendingRequestDetailsGUI("Pending Registration Request Details ");
					UnregisteredUser unRegModel = new UnregisteredUser();
					
					PendingRegistrationRequestDetailsController pendReqDetController = new PendingRegistrationRequestDetailsController(PendingRequestDetailsView,unRegModel);
					
					//add observer to new controller and delete the rest
					setCurrentObserver(pendReqDetController);
				
					//add a listener to "back" button in Pending Request Registration Details window
					setBackListener(previusObserver,PendingRequestDetailsView);
					
					//remove buttons from arrayList
					ButtonRequestModel.RemoveButtonRequest();
					
					String searchQuery = "search"	+"SELECT unRegCus.urcid,"
													+ "unRegCus.fname,unRegCus.lname,unRegCus.addr,"
													+ "unRegCus.phone,unRegCus.email,"
													+ "unRegCus.requestedPassword"
													+" FROM unregisteredcustomer AS unRegCus"
							                        +" WHERE unRegCus.urcid = " + Uid + "";
							                       
                    sendToServer(searchQuery);
                    
                    
				}
			}


			@Override
			public void handleDBRequestSuccess()
			{
				PendingRequestView.displayWrnMessage("Pending Registration Request Found:");
			}

			@Override
			public void handleDBSearchResult(CliMessage dbResult) 
			{	
				//get a list of all Registration Requests
				ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
				ArrayList<ButtonRequest> btnReq = ButtonRequestModel.getButtonRequest();
				int rows = srs.size();
				int column = 3;
				
				//create data table
				 String[][] data = new String[rows][column];
			   	
				 String[] columnNames={"Request Number", "First Name", "Last Name"};

				
				for(int i = 0 ; i <rows  ; i++)
				{
					HashMap<String, String> RequestList  = srs.get(i);	
					
					String reqID = RequestList.get("URCID");
					
                    //add uid & reqId button to arraylist
					btnReq.add(new ButtonRequest(reqID,reqID));
							
					//req num
					 data[i][0] = String.valueOf(i+1);
					 //fname
					 data[i][1] = RequestList.get("FNAME");
					 //lname
					 data[i][2] = RequestList.get("LNAME");
					
					 System.out.println("req id:"
							 			+RequestList.get("URCID")+" "+"user fname:"+RequestList.get("FNAME")
							 			+" "+"user lname:"+RequestList.get("LNAME"));
						 					
				}	
				
				//set new data table
				PendingRequestView.setTable(columnNames, data);
				
				//create n buttons
				int numberOfRequests = btnReq.size();
				
				PendingRequestView.setPendingRequestButtons(numberOfRequests);
				
				//Set list button  listener	
				setRequestsButtons(btnReq);
				 
				//display main window
				PendingRequestView.setVisible(true);
				PendingRequestView.setTableVisibilty(true);		
			}


			@Override
			public void handleNullSearchResult() 
			{
				//display main window
				PendingRequestView.setVisible(true);
				PendingRequestView.disposeTable();
				PendingRequestView.disposeExistingButtons();
				//msg
				PendingRequestView.displayWrnMessage("Pending Request Registration Not Found!!!");
				
			}
}
